﻿namespace DemoClient
{
    using AllWayNet.Common.Threading;
    using AllWayNet.Logger;
    using ServerUtils;
    using System;
    using System.Threading.Tasks;

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
    public class ServerInfoRequestProcess
    {
        private readonly TimerEx timer;
        private readonly ServerInfoServiceClientProxy proxy;
        private bool asyncCall = false;

        public ServerInfoRequestProcess(int interval)
        {
            this.proxy = new ServerInfoServiceClientProxy();
            this.timer = new TimerEx(interval, () => { this.Request(); });
        }

        public void Start()
        {
            this.timer.Start();
        }

        public void Stop()
        {
            this.timer.Stop();
        }

        private async void Request()
        {
            try
            {
                if (this.asyncCall)
                {
                    await this.RequestResourcesAsync(string.Empty);
                    await this.RequestResourcesAsync("failed-test");
                    await this.RequestResourcesAsync("server-error-test");
                }
                else
                {
                    this.RequestResources(string.Empty);
                    this.RequestResources("failed-test");
                    this.RequestResources("server-error-test");
                }

                asyncCall = !asyncCall;
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogError(ex, "Error in RequestResources.");
            }
        }

        private void RequestResources(string parameter)
        {
            var serverInfoResult = this.proxy.GetServerInfo(parameter);

            switch (serverInfoResult.ResultType)
            {
                case OperationResultType.Succeed:
                    var serverInfo = serverInfoResult.Data;
                    ApplicationLogger.LogInfo(
                        "GetServerInfo: Succeed, Server Name: {0}, Processor Time: {1}, Available Memory: {2} GB",
                        serverInfo.ServerName,
                        serverInfo.ProcessorTime,
                        serverInfo.AvailableMemory);
                    break;
                case OperationResultType.Failed:
                    ApplicationLogger.LogInfo(
                        "GetServerInfo: Failed, Message: {0}",
                        serverInfoResult.Message);
                    break;
                case OperationResultType.Error:
                    ApplicationLogger.LogInfo("GetServerInfo: ServerError");
                    break;
                default:
                    string text = string.Format("GetServerInfo. Invalid ResultType received. Value: {0}", serverInfoResult.ResultType);
                    throw new Exception(text);
            }
        }

        private async Task RequestResourcesAsync(string parameter)
        {
            var serverInfoResult = await this.proxy.GetServerInfoAsync(parameter);

            switch (serverInfoResult.ResultType)
            {
                case OperationResultType.Succeed:
                    var serverInfo = serverInfoResult.Data;
                    ApplicationLogger.LogInfo(
                        "GetServerInfoAsync: Succeed, Server Name: {0}, Processor Time: {1}, Available Memory: {2} GB",
                        serverInfo.ServerName,
                        serverInfo.ProcessorTime,
                        serverInfo.AvailableMemory);
                    break;
                case OperationResultType.Failed:
                    ApplicationLogger.LogInfo(
                        "GetServerInfoAsync: Failed, Message: {0}",
                        serverInfoResult.Message);
                    break;
                case OperationResultType.Error:
                    ApplicationLogger.LogInfo("GetServerInfoAsync: ServerError");
                    break;
                default:
                    string text = string.Format("GetServerInfoAsync. Invalid ResultType received. Value: {0}", serverInfoResult.ResultType);
                    throw new Exception(text);
            }
        }
    }
}
